package cn.zh.stan.activity;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import cn.zh.stan.BaseUrl;
import cn.zh.stan.MyApplication;
import cn.zh.stan.R;
import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

public class Balance extends BaseActivity {

    ImageView baback;
    LinearLayout recharg;
    MyApplication mapp;
    String uid;
    TextView batxt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_balance);
        mapp= (MyApplication) getApplication();
        uid=mapp.getSp().getString("uid","");
        initv();
         }



    public void initv(){
        baback= (ImageView) findViewById(R.id.baback);
        recharg= (LinearLayout)findViewById(R.id.recharg);
        batxt= (TextView) findViewById(R.id.batxt);
        baback.setOnClickListener(this);
        recharg.setOnClickListener(this);
         }


    @Override
    protected void onResume() {
        super.onResume();
        getBalance();
       }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        int id=view.getId();
        switch (id){
            case R.id.baback:
                finish();
                break;
            case R.id.recharg:
                Intent intent=new Intent(Balance.this,ReCharge.class);
                startActivity(intent);
                break;
             }
           }


    public void getBalance(){
        OkHttpUtils.get(BaseUrl.GetSurplus)
                .params("userId",uid)
                .execute(new StringCallback() {
                    @Override
                    public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                        try {
                            JSONObject js=new JSONObject(s);
                            boolean State=js.getBoolean("State");
                            if (State){
                               String  Surplus=js.getString("Surplus");
                                batxt.setText(Surplus);
                            }
                        } catch (JSONException e) {
                        }
                    }
                    @Override
                    public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                        super.onError(isFromCache, call, response, e);
                    }
                });
          }


    }
